/*
 * @lc app=leetcode.cn id=424 lang=cpp
 *
 * [424] 替换后的最长重复字符
 */

// @lc code=start
class Solution {
public:
    int characterReplacement(string s, int k) {
        int l = 0, r = 0;
        int maxChCount = 0; //出现次数最多的字母
        int res = 0;

        int ch[26] = { 0 };
        while (r < s.size()) {
            ch[s[r] - 'A']++;
            maxChCount = maxChCount > ch[s[r] - 'A'] ? maxChCount : ch[s[r] - 'A'];

            //字符串长度 - 重复次数 > 字母出现最多次数
            int strLen = r - l + 1;
            if (strLen - k > maxChCount) {
                ch[s[l] - 'A']--;
                l++;
            }
            
            strLen = r - l + 1;
            res = res > strLen ? res : strLen;
            r++;
        }

        return res;
    }
};
// @lc code=end

